﻿#ifndef DATAFACTORY_H
#define DATAFACTORY_H

#include <QObject>
#include <QWaitCondition>
#include <QMutex>
#include <QThread>
#include <QVector>
#include <QTcpSocket>
#include "include/global.h"
#include <QtExtSerialPort/qextserialport.h>
#include "circularQueue/circular_deque.h"
#include "../include/timebus.h"
#include "../include/taskthread.h"
#include "include/global.h"
class SQLThread;

class DataFactory : public QThread
{
    Q_OBJECT
public:
    explicit DataFactory(QObject *parent = 0);
    ~DataFactory();
    void run();   //thread
    void waitThread();  //thread clear

    //tcp socket
    QTcpSocket* getTcpSocket(){return tcpSocket;}
    void connectToServer(QString& addr, quint16 port);
    void shutDownConnect();
    void dontUseNet();
    void resetConnectToServer();
    const NetState getNetState(){return netState;}
    void setNetState(NetState ns){netState = ns;}
    void checkNetWhenUseNet();

    //serial port
     QextSerialPort* getSerialPort(){return serialPort;}
     bool  shutDownSerial();
     const UARTState getUARTState(){return uartState;}
     void setUARTState(UARTState us){uartState = us;}

    //MYSQL
     SQLThread* getSqlThread(){return sqlthread;}
     bool shutDownSQLConnect();
     const SQLState getSQLState(){return sqlState;}
    void setSQLState(SQLState ss){sqlState = ss;}

     TimeBus& getTimeBus(){return timeBus;}
	 bool getSimState(){
		 return isOnline;
	 }
	 void enSQL(int type, int stationNums, int sensorNo,float data);
/*!
timebus sendTime signal
net and uart sendData signal
All widget connect each other.
*/
private:
     //Net
     QTcpSocket* tcpSocket;
     NetState netState;
     QString serverIp;
     quint16 serverPort;
     int netCheckCount;
     bool useNet;

     //TimeBus
     TimeBus timeBus;//time bus

     //SQL
     SQLThread* sqlthread;//mysql thread
	 QString table;
     SQLState sqlState;
     //int sqlTryCnt;
	 //long oldTime;

     //Serial Port
     QextSerialPort* serialPort;//serial port
     UARTState uartState;
     int uartTryCnt;
     CircularDeque<char> circularQueue;
     QMutex mutex;
     QWaitCondition bufferIsEmpty;
     static const int FRAME_LENGTH = 11;
     static const char HEAD_FIRST = 'P';// (char)0xfb;
     static const char HEAD_SECOND = 'Z';//(char)0xfd;
     static const char TAIL_FIRST = 'T';//(char)0xfc;
     static const char TAIL_SECOND = 'Q';//(char)0xfe;

     //thread
     //线程关闭标记
     bool exitFlag;


	 bool isOnline; //SIM卡在线标识
	// QVector<QString> smsVec;
	 int smsNums;



     void initSomeValues();
	 bool isOk(QString& str);
	// bool checkSmsVecIsEmpty();
	 bool isOk(QByteArray& str);
	 int getNodeIndex(char ch);
	 int getNodeErrorType(char ch1, char ch2);
signals:
    void sendSqlState(const QString &);
    void sendTime(long t); //never be useds,defined in timebus
    void sendNetState(NetState);
    void sendUartState(UARTState);
    //statios no, sensor type, sensor no, id, data
    //void sendDataComingSignal(int,/* DataWindows*/quint8, long, long, float);
	void sendRemoteDataToMaiwindow(int stations, int type, int no, float data);//采集站+传感器类型+传感器编号+数据或状态
public slots:
    void onSerialReadyRead();
    void onNetReadyRead();
	void checkSimOnlineState();
	void sendMsgAT();
	void sendMsgCMGF();
	void sendMsgCNMI();
	void sendMsgRMALL();
	void sendMsgReadN(int n);
	void slove(char* str, int len);

};

#endif // DATAFACTORY_H
